import {GameType} from "../type/Type";
import {BroadcastRequest, BroadcastType, ControllerNo, GuiId,} from "../controller/model/ControllerOpt";
import Configer from "../frame/apiModel/config/Configer";
import {ConfigKey} from "../constant/ConfigConstant";
import {PlayerModel} from "../frame/apiModel/adapterModel/model4/PlayerModel";
import {UnitJson} from "../frame/apiModel/model/StoreJson";
import {ControllerBroadcast} from "../controller/ControllerBroadcast";
import {GuiController} from "../controller/GuiController";
import {PlayerDataKey} from "../frame/dataSource/DataSource";


export enum LoadRet {
    newUnit,
    newStart,
    loadAll,
}

export abstract class GameTypePolicyTemplate {
    loadRet: LoadRet;
    // fun中ret传入true代表已经加载了英雄  不需要再选择英雄
    chooseLoad: (player: PlayerModel, fun: (player: PlayerModel) => void) => void;
    // 1发生在选择英雄之前 2发生在选择英雄之后
    getLoadControllerNos1: (player: PlayerModel) => ControllerNo[];
    getLoadControllerNos2: (player: PlayerModel) => ControllerNo[];
    getSaveControllerNos: () => ControllerNo[];
}
class ORPGGameTypePolicy extends GameTypePolicyTemplate {
    loadRet = LoadRet.loadAll;

    getLoadControllerNos1 = (player: PlayerModel) => {
        return [
            ControllerNo.UnitController,
            ControllerNo.AchievementController,
            ControllerNo.StoreController,
        ]
    }
    getLoadControllerNos2 = (player: PlayerModel) => {
        return [
            ControllerNo.GoodController,
            ControllerNo.AbilityController,
            ControllerNo.TaskController,
        ]
    }
    getSaveControllerNos = () => {
        return [
            ControllerNo.UnitController,
            ControllerNo.GoodController,
            ControllerNo.AbilityController,
            ControllerNo.AchievementController,
            ControllerNo.TaskController,
            ControllerNo.StoreController,
        ]
    }
    chooseLoad = (player: PlayerModel, fun: (player: PlayerModel) => void) => {
        const unitJsons: UnitJson[] = [];
        const unitSerializeBody = player.data[PlayerDataKey.storeHistory][ControllerNo.UnitController];

        const heroInfo0: UnitJson = unitSerializeBody.hi1;
        const heroInfo1: UnitJson = unitSerializeBody.hi2;
        const heroInfo2: UnitJson = unitSerializeBody.hi3;

        if (heroInfo0) {
            unitJsons[0] = heroInfo0;
        } else {
            unitJsons[0] = {} as any;
        }

        if (heroInfo1) {
            unitJsons[1] = heroInfo1;
        } else {
            unitJsons[1] = {} as any;
        }

        if (heroInfo2) {
            unitJsons[2] = heroInfo2;
        } else {
            unitJsons[2] = {} as any;
        }

        const request: BroadcastRequest = { broadcastType: BroadcastType.showUi, controllerNo: ControllerNo.GuiController, data: { player: player, guiId: GuiId.chooseArchiveBar, data: { unitJsons: unitJsons } } }
        ControllerBroadcast.getInstance().broadcast(request);
    }
}
class LIVEGameTypePolicy extends GameTypePolicyTemplate {
    loadRet = LoadRet.newUnit
    getLoadControllerNos1 = (player: PlayerModel) => {
        return [
            ControllerNo.AchievementController,
            ControllerNo.TaskController,
            ControllerNo.StoreController,
        ]
    }
    getLoadControllerNos2 = (player: PlayerModel) => {
        return [
        ]
    }
    getSaveControllerNos = () => {
        return [
            ControllerNo.AchievementController,
            ControllerNo.TaskController,
            ControllerNo.StoreController,
        ]
    }
    chooseLoad = (player: PlayerModel, fun: (player: PlayerModel) => void) => {
        fun(player);
    }
}
/**
   ORPG: orpg地图同一个存档中的内容需要同时读取，并且需要支持清空存档的功能，并且初始任务的创建在有存档和无存档时走不同的流程
   LIVE: 生存图这种地图一般只需要保存成就 任务等内容，并且一般不需要清空存档，无论是否有存档，都需要重新创建人物
 */
class GameTypeFactory {
    getGameTypePolicy (gameType: GameType) {
        switch (gameType) {
            case GameType.live:{
                return new LIVEGameTypePolicy();
            }
            case GameType.orpg:{
                return new ORPGGameTypePolicy();
            }
        }
    }
}

const gameType = Configer.getConfig(ConfigKey.gameType) as number
export const gameTypePolicy = new GameTypeFactory().getGameTypePolicy(gameType);